import UIManager from "../UIManager";
import Util from "../utils/Util";
import UIBase from "./UIBase";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StartMenu extends UIBase {
    @property(cc.Node)
    startButton: cc.Node = null
    @property(cc.Node)
    startButtonImage: cc.Node = null

    @property(cc.Node)
    levelSelectButton: cc.Node = null

    private _uiManager: UIManager = null

    onLoad() {
        super.onLoad()
        console.log(`StartMenu onLoad()`)
    }

    public init(uiManager: UIManager): void {
        this._uiManager = uiManager
        const { TOUCH_START, TOUCH_END, TOUCH_CANCEL } = cc.Node.EventType
        // 按键事件
        // 开始按钮
        this.startButton.on(TOUCH_START, () => {
            Util.clickDownTween(this.startButton)
        })
        this.startButton.on(TOUCH_END, () => {
            Util.clickUpTween(this.startButton, () => {
                this._uiManager.startGame(1)
            })

        })
        this.startButton.on(TOUCH_CANCEL, () => {
            Util.clickUpTween(this.startButton)
        })
        // 关卡选择按钮
        this.levelSelectButton.on(TOUCH_START, () => {
            Util.clickDownTween(this.levelSelectButton)
        })
        this.levelSelectButton.on(TOUCH_END, () => {
            Util.clickUpTween(this.levelSelectButton, () => {
                this._uiManager.toLevelSelect()
            })

        })
        this.levelSelectButton.on(TOUCH_CANCEL, () => {
            Util.clickUpTween(this.levelSelectButton)
        })
    }

    public show(): void {
        super.show()
        // 开始按钮上的饼干动画效果
        this._cookieAnimation()
    }

    /**
     * 开始按钮上的饼干动画效果
     */
    private _cookieAnimation(): void {
        const cookieNode = this.startButton.children[0]
        // 初始化动画状态
        cookieNode.stopAllActions()
        cookieNode.angle = 0
        // 循环播放
        cc.tween(cookieNode).repeatForever(
            // 角度旋转
            cc.tween()
                .to(1, { angle: 10 })
                .to(1, { angle: 0 })
        ).start()
    }

}
